package com.sangmo.fts.basic.service;

import com.sangmo.boot.framework.plugin.dataview.crud.service.BaseViewService;
import com.sangmo.boot.framework.plugin.dataview.data.IDataViewRepo;
import com.sangmo.fts.basic.model.DocumentTemplate;
import com.sangmo.fts.basic.model.FileUpload;
import com.sangmo.fts.basic.repo.DocumentTemplateRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class DocumentTemplateEntityService extends BaseViewService<DocumentTemplate> {

    @Autowired
    private DocumentTemplateRepo documentTemplateRepo;

    @Autowired
    private FileUploadService fileUploadService;

    @Override
    protected IDataViewRepo<DocumentTemplate> getRepo() {
        return documentTemplateRepo;
    }

    @Override
    protected void onCreate(DocumentTemplate entity, Object bo) {
        super.onCreate(entity, bo);
        FileUpload file = fileUploadService.find(entity.getDocId());
        String fileName = file.getFileName() == null?"":file.getFileName();
        entity.setType(fileName.indexOf(".") == -1 ? "" : (fileName.substring(fileName.lastIndexOf(".")+1)));
    }

    @Override
    protected void afterCreated(DocumentTemplate entity, Object bo) {
        super.afterCreated(entity, bo);
        fileUploadService.updateRef(entity.getId(), ""+entity.getId(), "DocumentTemplate");
    }
}
